<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <div class="logo">后台管理系统</div>
      <el-menu
        :router="true"
        class="el-menu-vertical"
      >
        <template v-if="isAdmin">
          <el-menu-item index="/admin">
            <el-icon><Odometer /></el-icon>
            <span>控制台</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>

          <el-menu-item index="/admin/scales">
            <el-icon><List /></el-icon>
            <span>量表管理</span>
          </el-menu-item>

          <el-menu-item index="/admin/assessments">
            <el-icon><DataAnalysis /></el-icon>
            <span>测评记录</span>
          </el-menu-item>

          <el-menu-item index="/admin/resources">
            <el-icon><Reading /></el-icon>
            <span>教育资源</span>
          </el-menu-item>
        </template>
        <template v-if="isConsultant">
          <el-menu-item index="/admin/consultations">
          <el-icon><ChatDotRound /></el-icon>
          <span>咨询记录</span>
        </el-menu-item>
        </template>

      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-avatar :size="32" :src="userInfo?.avatar || '/default-avatar.png'" />
              {{ userInfo?.username }}
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="home">返回首页</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import Cookies from 'js-cookie'
import {
  Odometer,
  User,
  List,
  Document,
  DataAnalysis,
  Reading,
  ChatDotRound
} from '@element-plus/icons-vue'

const router = useRouter()

const userInfo = computed(() => {
  const info = Cookies.get('userInfo')
  return info ? JSON.parse(info) : null
})

const isAdmin = computed(() => userInfo.value?.role === 'ADMIN')
const isConsultant = computed(() => userInfo.value?.isConsultant == 1)

const handleCommand = (command) => {
  switch (command) {
    case 'home':
      router.push('/')
      break
    case 'logout':
      Cookies.remove('token')
      Cookies.remove('userInfo')
      ElMessage.success('退出成功')
      router.push('/login')
      break
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-aside {
  background-color: #304156;
  color: #fff;
}

.logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  background-color: #2b2f3a;
}

.el-menu-vertical {
  border-right: none;
  background-color: #304156;
}

.el-menu-vertical :deep(.el-menu-item) {
  color: #bfcbd9;
}

.el-menu-vertical :deep(.el-menu-item.is-active) {
  color: #409EFF;
}

.el-menu-vertical :deep(.el-menu-item:hover) {
  color: #fff;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style> 